package com.wxy.ej.flow.modules.excelOnline.controller.beanController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.base.MyQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.base.PageDto;
import com.wxy.ej.flow.modules.excelOnline.dto.ExcelOnlineDto;
import com.wxy.ej.flow.modules.excelOnline.bean.ExcelOnline;
import com.wxy.ej.flow.modules.excelOnline.service.beanService.ExcelOnlineService;
import java.util.List;

/**
* 前端控制器
* 1，每个bean的Controller, 根据实际情况，查以删除不用的方法
* 2，用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，userDetailsJson)
* @author wuxiaoyong
* @since 2022-05-14
*/
@Slf4j
@Api(tags = "ExcelOnlineController -  控制类")
@RequestMapping("/coding/excelOnline")
@Controller
public class ExcelOnlineController {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected ExcelOnlineService excelOnlineService;

    /*@PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("分页查询列表（适合单表查询）")
    @GetMapping(value = "/page")
    @ResponseBody
    public Result page(@ModelAttribute ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "excelOnline:query");
        Page<ExcelOnline> page = new Page<>(excelOnlineDto.getPage(), excelOnlineDto.getLimit());

        // 查询条件
        ExcelOnline excelOnlineQuery = mapper.map(excelOnlineDto, ExcelOnline.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        excelOnlineQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        excelOnlineQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<ExcelOnline> queryWrapper=new QueryWrapper<>(excelOnlineQuery);
        //特殊查询条件
        queryWrapper.ge(excelOnlineDto.getCreateTime_Start()!=null,"create_time",excelOnlineDto.getCreateTime_Start());
        queryWrapper.le(excelOnlineDto.getCreateTime_End()!=null,"create_time",excelOnlineDto.getCreateTime_End());
        // queryWrapper.orderBy(excelOnlineDto.getCondition(), excelOnlineDto.isAsc(), excelOnlineDto.getColumn());
        // 支持前端：listQuery.sort='-part_no,-id'
        for (PageDto sortDto:excelOnlineDto.getSortList()){
            queryWrapper.orderBy(true, sortDto.getAsc(), sortDto.getColumn());
        }

        return excelOnlineService.page(page, queryWrapper);
    }*/

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    /*@PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("以ID作为父节点，查询子树")
    @GetMapping(value = "/tree")
    @ResponseBody
    public Result tree (Long id) {
        Result<ExcelOnlineDto> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        ExcelOnlineDto excelOnlineDto = new ExcelOnlineDto();
        excelOnlineDto.setId(0L);
        if (id!=0) {
            ExcelOnline excelOnline = excelOnlineService.getById(id);
            excelOnlineDto = mapper.map(excelOnline, ExcelOnlineDto.class);
        }
        treeReduce(excelOnlineDto);
        result.setObj(excelOnlineDto);

        return result.success();
    }

    private void treeReduce(ExcelOnlineDto excelOnlineDto){
    }*/

    @PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("自定义sql分页查询（mapper.xml, 适合多表查询）")
    @GetMapping(value = "/myPage")
    @ResponseBody
    public Result myPage(@ModelAttribute ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "excelOnline:query");
        Page<ExcelOnline> page = new Page<>(excelOnlineDto.getPage(), excelOnlineDto.getLimit());

        // 查询条件
        ExcelOnline excelOnlineQuery = mapper.map(excelOnlineDto, ExcelOnline.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        excelOnlineQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        excelOnlineQuery.rebuildDataLimitQueryBean(userDetailsDto);

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<ExcelOnline> queryWrapper = MyQueryWrapper.buildQueryWrapper(excelOnlineQuery , "create_time");
        //特殊查询条件
        queryWrapper.ge(excelOnlineDto.getCreateTime_Start()!=null,"t1.create_time",excelOnlineDto.getCreateTime_Start());
        queryWrapper.le(excelOnlineDto.getCreateTime_End()!=null,"t1.create_time",excelOnlineDto.getCreateTime_End());
        // queryWrapper.orderBy(excelOnlineDto.getCondition(), excelOnlineDto.isAsc(), "t1."+excelOnlineDto.getColumn());
        // 支持前端：listQuery.sort='-part_no,-id'
        for (PageDto sortDto:excelOnlineDto.getSortList()){
            queryWrapper.orderBy(true, sortDto.getAsc(), "t1."+sortDto.getColumn());
        }

        return excelOnlineService.myPage(page, queryWrapper, excelOnlineDto.getHasFooter());
    }



    @PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("查询所有")
    @GetMapping(value = "/selectBy")
    @ResponseBody
    public Result selectBy(@ModelAttribute ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "excelOnline:query");
        Result<ExcelOnlineDto> result = new Result<>();

        // 查询条件
        ExcelOnline excelOnlineQuery = mapper.map(excelOnlineDto, ExcelOnline.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        excelOnlineQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        excelOnlineQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<ExcelOnline> queryWrapper=new QueryWrapper<>(excelOnlineQuery);
        //特殊查询条件
        queryWrapper.ge(excelOnlineDto.getCreateTime_Start()!=null,"create_time",excelOnlineDto.getCreateTime_Start());
        queryWrapper.le(excelOnlineDto.getCreateTime_End()!=null,"create_time",excelOnlineDto.getCreateTime_End());
        queryWrapper.orderBy(excelOnlineDto.getCondition(), excelOnlineDto.isAsc(), excelOnlineDto.getColumn());

        result = excelOnlineService.selectBy(queryWrapper);

        return result;
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("selectById")
    @GetMapping(value = "/selectById")
    @ResponseBody
    public Result selectById(Long id){
        return excelOnlineService.selectById(id);
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("SQL动态多选项, spring动态调用")
    @GetMapping(value = "/select4column")
    @ResponseBody
    public Result select4column(String columnName){
        return excelOnlineService.select4column(columnName);
    }

    /*@PreAuthorize("hasAuthority('taskflow:excelOnline:create')")
    @ApiOperation("新增对象")
    @PostMapping(value = "/create")
    @ResponseBody
    public Result create(@RequestBody ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        //插入参数
        excelOnlineDto.setCreatorUpdatorInfo(userDetailsJson);

        //调用服务
        return excelOnlineService.create(excelOnlineDto);
    }*/

    @PreAuthorize("hasAuthority('taskflow:excelOnline:update')")
    @ApiOperation("编辑对象")
    @PostMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestBody ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        try{
            excelOnlineDto.getId().longValue();
        }catch (Exception e){
            return Result.builder().fault("数据中缺少ID");
        }

        //参数
        excelOnlineDto.setUpdatorInfo(userDetailsJson);

        //调用服务
        return excelOnlineService.updateByDtoId(excelOnlineDto);
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:delete')")
    @ApiOperation("逻辑删除对象，由deleteflag控制：0:有效，1:已删除。")
    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(Long id, String userDetailsJson){
        try{
            id.longValue();
        }catch (Exception e){
            return Result.builder().fault("数据中缺少ID");
        }
        //参数
        ExcelOnlineDto excelOnlineDto = new ExcelOnlineDto();
        excelOnlineDto.setUpdatorInfo(userDetailsJson);
        excelOnlineDto.setId(id);
        excelOnlineDto.setDeleteFlag(1);

        //调用服务
        return excelOnlineService.updateByDtoId(excelOnlineDto);
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:deletePhysical')")
    @ApiOperation("物理删除对象，默认注释了核心调用方法，请按需启用")
    @PostMapping(value = "/deletePhysical")
    @ResponseBody
    public Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//excelOnlineService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
